﻿

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.Model;
using Gift.DAL;

namespace WebApp.admin.goods
{
    public partial class keywords : BaseAdmin
    {
        // Fields


        tbKeyWords oKeyWords = new tbKeyWords();
        readonly DALKeyWords theKeyWords = new DALKeyWords();

        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                this.oKeyWords.keyWord = this.txtKeyWord.Value;
                this.oKeyWords.clickCount = Convert.ToInt32(this.txtClickCount.Value);
                if (this.rdIsShow0.Checked)
                {
                    this.oKeyWords.isShow = false;
                }
                else
                {
                    this.oKeyWords.isShow = true;
                }
            
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.oKeyWords.id = Convert.ToInt32(base.Request.QueryString["id"]);
                    if (this.theKeyWords.Update(this.oKeyWords))
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"hotKeywordsList.aspx\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                    }
                }
                else if (this.theKeyWords.Add(this.oKeyWords) > 0)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"hotKeywordsList.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
        }

        public StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if (base.Request.QueryString["ot"] == null)
                {
                    return builder;
                }
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    builder.Append("编辑关键词");
                    return builder;
                }
                builder.Append("添加关键词");
            }
            catch
            {
            }
            return builder;
        }

        private void KeyWordsBind()
        {
            try
            {
                oKeyWords = this.theKeyWords.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                if (oKeyWords != null)
                {
                    this.txtKeyWord.Value = oKeyWords.keyWord;
                    this.txtClickCount.Value = oKeyWords.clickCount.ToString();
                    if (oKeyWords.isShow == false)
                    {
                        this.rdIsShow1.Checked = true;
                    }
                    else
                    {
                        this.rdIsShow0.Checked = true;
                    }
                }
            }
            catch
            {
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (base.Request.QueryString["ot"] == "ed")
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.HotKeywordsEdit);
                }
                else
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.HotKeywordsAdd);
                }
                if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
                {
                    if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                    {
                        this.KeyWordsBind();
                    }
                    else
                    {
                        this.rdIsShow1.Checked = true;
                    }
                }
            }
        }

    }
}